package com.famiover.generator.utils;

import org.apache.commons.io.output.FileWriterWithEncoding;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import java.io.File;
import java.util.Properties;


public class VelocityUtils {

    public static void generator(String inputVmFilePath, String outputFilePath, VelocityContext context) throws Exception {
        Properties properties = new Properties();
        properties.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, getPath(inputVmFilePath));
        Velocity.init(properties);
        Template template = Velocity.getTemplate(getFile(inputVmFilePath), "utf-8");
        File outputFile = new File(outputFilePath);
        FileWriterWithEncoding writer = new FileWriterWithEncoding(outputFile, "utf-8");
        template.merge(context, writer);
        writer.close();
    }


    public static String getPath(String filePath) {
        String path = "";
        if (Utils.isNotEmpty(filePath)) {
            path = filePath.substring(0, filePath.lastIndexOf("/") + 1);
        }
        return path;
    }


    public static String getFile(String filePath) {
        String file = "";
        if (Utils.isNotEmpty(filePath)) {
            file = filePath.substring(filePath.lastIndexOf("/") + 1);
        }
        return file;
    }

}
